package com.laputa.dao.hibernate;

import java.util.*;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.laputa.dao.BuildingDao;
import com.laputa.dao.model.Building;

public class BuildingDaoHibernate extends HibernateDaoSupport implements BuildingDao{
	
	public void delete(Long id) {
		this.getHibernateTemplate().delete(getBuilding(id));
	}

	public List getAll() {
		return this.getHibernateTemplate().find("from Building");
	}

	public Building getBuilding(Long id) {
		return (Building)this.getHibernateTemplate().get(Building.class, id);
	}
	
	//TODO: to be removed cause holder represent the ground not user
	public List getBuildings(Long holderId){
		List buildingList = this.getHibernateTemplate().find("from Building b where b.holder = '" + holderId +"'");
		return buildingList;
	}
	
	public Building getBuildingByHolder(Long holderId){
		List buildingList;
		if ((buildingList = this.getHibernateTemplate().find("from Building b where b.holder = " + holderId))!= null){
			return (Building)buildingList.get(0);
		}
		else 
			return null;
	}
	
	public void save(Building b) {
	    this.getHibernateTemplate().saveOrUpdate(b);
	}

}
